package com.chang.springsecurity.security.service;


import com.chang.springsecurity.entity.User;
import com.chang.springsecurity.repository.UserRepository;
import com.chang.springsecurity.security.model.SecurityUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String phone) {
        User user = userRepository.findByPhone(phone);
        if (user == null || !user.isEnabled()){
            throw new UsernameNotFoundException("用户不存在或已被禁用："+phone);
        }
        return new SecurityUserDetails(user);
    }
}
